package org.afs.gescol.gwt.client.view;

import java.util.HashMap;
import java.util.Map;

import org.afs.gescol.gwt.client.bundle.GescolResources;
import org.afs.gescol.gwt.client.factory.AppClientFactory;
import org.afs.gescol.gwt.client.i18n.AppMessages;
import org.afs.gescol.gwt.client.presenter.ElevePresenter.EleveView;
import org.afs.gescol.gwt.client.presenter.ElevePresenter.PANELS_ELEVE_KEYS;
import org.afs.gescol.gwt.client.util.ConstantesKeys;
import org.afs.gescol.gwt.shared.AbsPermModel;
import org.afs.gescol.gwt.shared.EleveDetailsModel;
import org.afs.gescol.gwt.shared.FraisScoModel;
import org.afs.gescol.gwt.shared.NoteModel;
import org.afs.gescol.gwt.shared.ParcoursModel;
import org.afs.gescol.gwt.shared.ParentModel;
import org.scub.foundation.framework.gwt.module.client.util.composants.box.datetime.DateBox;
import org.scub.foundation.framework.gwt.module.client.util.composants.padding.VerticalPanelPadding;
import org.scub.foundation.framework.gwt.module.client.util.composants.suggestbox.SuggestListBoxSingle;
import org.scub.foundation.framework.gwt.module.client.util.composants.suggestbox.SuggestListBoxSingle.SuggestListBoxSingleProperties;
import org.scub.foundation.framework.gwt.module.shared.IdLabelModel;

import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.DoubleBox;
import com.github.gwtbootstrap.client.ui.FormLabel;
import com.github.gwtbootstrap.client.ui.Image;
import com.github.gwtbootstrap.client.ui.Label;
import com.github.gwtbootstrap.client.ui.TextArea;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.github.gwtbootstrap.client.ui.constants.IconType;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;

public class EleveViewImpl extends Composite implements EleveView {

    private EleveViewBinder binder = AppClientFactory.getEleveViewBinder();

    private AppMessages messages = AppClientFactory.getMessages();

    private GescolResources resources = AppClientFactory.getResources();

    @UiField
    Button btnDepilerIdentite;

    @UiField
    Button btnEnregistrerIdentite;

    @UiField
    Button btnToutEnregistrerIdentite;

    @UiField
    Button btnChargerPhoto;

    @UiField
    Button btnAjouterPJ;

    @UiField
    Button btnDepilerParents;

    @UiField
    Button btnEnregistrerParents;

    @UiField
    Button btnToutEnregistrerParents;

    @UiField
    Button btnDepilerAbsPerm;

    @UiField
    Button btnEnregistrerAbsPerm;

    @UiField
    Button btnToutEnregistrerAbsPerm;

    @UiField
    Button btnVisualiserAbsPerm;

    @UiField
    Button btnAjouterAbsPerm;

    @UiField
    Button btnDepilerFraisSco;

    @UiField
    Button btnEnregistrerFraisSco;

    @UiField
    Button btnToutEnregistrerFraisSco;

    @UiField
    Button btnVisualiserFraisSco;

    @UiField
    Button btnAjouterFraisSco;

    @UiField
    Button btnDepilerNoteBul;

    @UiField
    Button btnEnregistrerNoteBul;

    @UiField
    Button btnToutEnregistrerNoteBul;

    @UiField
    Button btnVisualiserNoteBul;

    @UiField
    Button btnImprimerBul;

    @UiField
    Button btnDepilerParcours;

    @UiField
    Button btnImprimerParcours;

    @UiField
    TextArea tbObservations;

    @UiField(provided = true)
    SuggestListBoxSingle<IdLabelModel> slbClasse;

    @UiField(provided = true)
    SuggestListBoxSingle<IdLabelModel> slbRedoublant;

    @UiField(provided = true)
    SuggestListBoxSingle<IdLabelModel> slbSexe;

    @UiField(provided = true)
    SuggestListBoxSingle<IdLabelModel> slbStatut;

    @UiField(provided = true)
    SuggestListBoxSingle<IdLabelModel> slbPeriodeAbsPerm;

    @UiField(provided = true)
    SuggestListBoxSingle<IdLabelModel> slbPeriodeNoteBul;

    @UiField(provided = true)
    SuggestListBoxSingle<IdLabelModel> slbPeriodeFraisSco;

    @UiField(provided = true)
    Image photoEleve;

    @UiField
    TextBox tbMatricule;

    @UiField
    TextBox tbNom;

    @UiField
    TextBox tbPrenoms;

    @UiField
    TextBox tbLieuNais;

    @UiField
    TextBox tbCourriel;

    @UiField
    TextBox tbAdresse;

    @UiField
    TextBox tbNomPere;

    @UiField
    TextBox tbPrenomsPere;

    @UiField
    TextBox tbAdressePere;

    @UiField
    TextBox tbTelPere;

    @UiField
    TextBox tbNomMere;

    @UiField
    TextBox tbPrenomsMere;

    @UiField
    TextBox tbAdresseMere;

    @UiField
    TextBox tbTelMere;

    @UiField
    TextBox tbNomTuteur;

    @UiField
    TextBox tbPrenomsTuteur;

    @UiField
    TextBox tbAdresseTuteur;

    @UiField
    TextBox tbTelTuteur;

    @UiField
    DateBox dbDateEntree;

    @UiField
    DateBox dbDateSortie;

    @UiField
    DateBox dbDateNais;

    @UiField
    HorizontalPanel panelListAbsPerm;

    @UiField
    HorizontalPanel panelListFraisSco;

    @UiField
    HorizontalPanel panelListNoteBul;

    @UiField
    HorizontalPanel panelListParcours;

    @UiField
    VerticalPanelPadding panelListePJ;

    @UiField
    HTMLPanel panelIdentite;

    @UiField
    HTMLPanel panelParents;

    @UiField
    HTMLPanel panelAbsPerm;

    @UiField
    HTMLPanel panelFraisSco;

    @UiField
    HTMLPanel panelNoteBul;

    @UiField
    HTMLPanel panelParcours;

    private FlexTable fxTableAbsPerm = new FlexTable();

    private FlexTable fxTableFraisSco = new FlexTable();

    private FlexTable fxTableNoteBul = new FlexTable();

    private FlexTable fxTableParcours = new FlexTable();

    /**
     * An interface required by the UiBinder.
     */
    public interface EleveViewBinder extends UiBinder<Widget, EleveViewImpl> {
    }

    @Override
    public void init() {
        slbClasse = new SuggestListBoxSingle<IdLabelModel>(new SuggestListBoxSingleProperties<IdLabelModel>() {
            @Override
            public String getSelectSuggestRenderer(IdLabelModel row) {
                return row != null && row.getLabel() != null ? row.getLabel() : "";
            }
            @Override
            public String[] getResultColumnsRenderer() {
                return new String[] {messages.lblLibelle()};
            }
            @Override
            public String[] getResultRowsRenderer(IdLabelModel row) {
                return new String[] {row != null && row.getLabel() != null ? row.getLabel() : ""};
            }
        });
        slbClasse.setWidth(AppMessages.PIXELS_70);
        slbRedoublant = new SuggestListBoxSingle<IdLabelModel>(new SuggestListBoxSingleProperties<IdLabelModel>() {
            @Override
            public String getSelectSuggestRenderer(IdLabelModel row) {
                return row != null && row.getLabel() != null ? row.getLabel() : "";
            }
            @Override
            public String[] getResultColumnsRenderer() {
                return new String[] {messages.lblLibelle()};
            }
            @Override
            public String[] getResultRowsRenderer(IdLabelModel row) {
                return new String[] {row != null && row.getLabel() != null ? row.getLabel() : ""};
            }
        });
        slbRedoublant.setWidth(AppMessages.PIXELS_70);
        slbSexe = new SuggestListBoxSingle<IdLabelModel>(new SuggestListBoxSingleProperties<IdLabelModel>() {
            @Override
            public String getSelectSuggestRenderer(IdLabelModel row) {
                return row != null && row.getLabel() != null ? row.getLabel() : "";
            }
            @Override
            public String[] getResultColumnsRenderer() {
                return new String[] {messages.lblLibelle()};
            }
            @Override
            public String[] getResultRowsRenderer(IdLabelModel row) {
                return new String[] {row != null && row.getLabel() != null ? row.getLabel() : ""};
            }
        });
        slbSexe.setWidth(AppMessages.PIXELS_70);
        slbStatut = new SuggestListBoxSingle<IdLabelModel>(new SuggestListBoxSingleProperties<IdLabelModel>() {
            @Override
            public String getSelectSuggestRenderer(IdLabelModel row) {
                return row != null && row.getLabel() != null ? row.getLabel() : "";
            }
            @Override
            public String[] getResultColumnsRenderer() {
                return new String[] {messages.lblLibelle()};
            }
            @Override
            public String[] getResultRowsRenderer(IdLabelModel row) {
                return new String[] {row != null && row.getLabel() != null ? row.getLabel() : ""};
            }
        });
        slbStatut.setWidth(AppMessages.PIXELS_70);
        slbPeriodeAbsPerm = new SuggestListBoxSingle<IdLabelModel>(new SuggestListBoxSingleProperties<IdLabelModel>() {
            @Override
            public String getSelectSuggestRenderer(IdLabelModel row) {
                return row != null && row.getLabel() != null ? row.getLabel() : "";
            }
            @Override
            public String[] getResultColumnsRenderer() {
                return new String[] {messages.lblLibelle()};
            }
            @Override
            public String[] getResultRowsRenderer(IdLabelModel row) {
                return new String[] {row != null && row.getLabel() != null ? row.getLabel() : ""};
            }
        });
        slbPeriodeAbsPerm.setWidth(AppMessages.PIXELS_100);
        slbPeriodeFraisSco = new SuggestListBoxSingle<IdLabelModel>(new SuggestListBoxSingleProperties<IdLabelModel>() {
            @Override
            public String getSelectSuggestRenderer(IdLabelModel row) {
                return row != null && row.getLabel() != null ? row.getLabel() : "";
            }
            @Override
            public String[] getResultColumnsRenderer() {
                return new String[] {messages.lblLibelle()};
            }
            @Override
            public String[] getResultRowsRenderer(IdLabelModel row) {
                return new String[] {row != null && row.getLabel() != null ? row.getLabel() : ""};
            }
        });
        slbPeriodeFraisSco.setWidth(AppMessages.PIXELS_100);
        slbPeriodeNoteBul = new SuggestListBoxSingle<IdLabelModel>(new SuggestListBoxSingleProperties<IdLabelModel>() {
            @Override
            public String getSelectSuggestRenderer(IdLabelModel row) {
                return row != null && row.getLabel() != null ? row.getLabel() : "";
            }
            @Override
            public String[] getResultColumnsRenderer() {
                return new String[] {messages.lblLibelle()};
            }
            @Override
            public String[] getResultRowsRenderer(IdLabelModel row) {
                return new String[] {row != null && row.getLabel() != null ? row.getLabel() : ""};
            }
        });
        slbPeriodeNoteBul.setWidth(AppMessages.PIXELS_100);
        photoEleve = new Image(resources.noPhoto());
        initWidget(binder.createAndBindUi(this));
        btnEnregistrerIdentite.setEnabled(true);
        btnToutEnregistrerIdentite.setEnabled(true);
        btnEnregistrerParents.setEnabled(false);
        btnToutEnregistrerParents.setEnabled(false);
        btnEnregistrerAbsPerm.setEnabled(false);
        btnToutEnregistrerAbsPerm.setEnabled(false);
        btnEnregistrerFraisSco.setEnabled(false);
        btnToutEnregistrerFraisSco.setEnabled(false);
        btnEnregistrerNoteBul.setEnabled(false);
        btnToutEnregistrerNoteBul.setEnabled(false);
        btnImprimerParcours.setEnabled(false);
        panelIdentite.setVisible(true);
        panelParents.setVisible(false);
        panelAbsPerm.setVisible(false);
        panelFraisSco.setVisible(false);
        panelNoteBul.setVisible(false);
        panelParcours.setVisible(false);
        createFlexTableDetails();
    }

    private void createFlexTableDetails() {
        fxTableAbsPerm.setWidth(AppMessages.PERCENT_100);
        fxTableAbsPerm.setWidget(0, 0, new FormLabel(messages.type()));
        fxTableAbsPerm.setWidget(0, 1, new FormLabel(messages.dateDebut()));
        fxTableAbsPerm.setWidget(0, 2, new FormLabel(messages.dateFin()));
        fxTableAbsPerm.setWidget(0, 3, new FormLabel(messages.justifiee()));
        fxTableAbsPerm.setWidget(0, 4, new FormLabel(messages.motifs()));
        fxTableAbsPerm.getColumnFormatter().setWidth(0, AppMessages.PERCENT_10);
        fxTableAbsPerm.getColumnFormatter().setWidth(1, AppMessages.PERCENT_10);
        fxTableAbsPerm.getColumnFormatter().setWidth(2, AppMessages.PERCENT_10);
        fxTableAbsPerm.getColumnFormatter().setWidth(3, AppMessages.PERCENT_10);
        fxTableAbsPerm.getColumnFormatter().setWidth(4, AppMessages.PERCENT_60);
        fxTableAbsPerm.getRowFormatter().addStyleName(0, resources.css().headerDescTable());
        panelListAbsPerm.setWidth(AppMessages.PERCENT_100);
        panelListAbsPerm.add(fxTableAbsPerm);

        fxTableFraisSco.setWidth(AppMessages.PERCENT_100);
        fxTableFraisSco.setWidget(0, 0, new FormLabel(messages.tranche()));
        fxTableFraisSco.setWidget(0, 1, new FormLabel(messages.datePaiement()));
        fxTableFraisSco.setWidget(0, 2, new FormLabel(messages.montantDu()));
        fxTableFraisSco.setWidget(0, 3, new FormLabel(messages.montantPaye()));
        fxTableFraisSco.setWidget(0, 4, new FormLabel(messages.montantRestant()));
        fxTableFraisSco.getColumnFormatter().setWidth(0, AppMessages.PERCENT_20);
        fxTableFraisSco.getColumnFormatter().setWidth(1, AppMessages.PERCENT_20);
        fxTableFraisSco.getColumnFormatter().setWidth(2, AppMessages.PERCENT_20);
        fxTableFraisSco.getColumnFormatter().setWidth(3, AppMessages.PERCENT_20);
        fxTableFraisSco.getColumnFormatter().setWidth(4, AppMessages.PERCENT_20);
        fxTableFraisSco.getRowFormatter().addStyleName(0, resources.css().headerDescTable());
        panelListFraisSco.setWidth(AppMessages.PERCENT_100);
        panelListFraisSco.add(fxTableFraisSco);

        fxTableNoteBul.setWidth(AppMessages.PERCENT_100);
        fxTableNoteBul.setWidget(0, 0, new FormLabel(messages.matieres()));
        fxTableNoteBul.setWidget(0, 1, new FormLabel(messages.note1()));
        fxTableNoteBul.setWidget(0, 2, new FormLabel(messages.note2()));
        fxTableNoteBul.setWidget(0, 3, new FormLabel(messages.note3()));
        fxTableNoteBul.setWidget(0, 4, new FormLabel(messages.noteCompo()));
        fxTableNoteBul.setWidget(0, 5, new FormLabel(messages.appreciation()));
        fxTableNoteBul.getColumnFormatter().setWidth(0, AppMessages.PERCENT_20);
        fxTableNoteBul.getColumnFormatter().setWidth(1, AppMessages.PERCENT_10);
        fxTableNoteBul.getColumnFormatter().setWidth(2, AppMessages.PERCENT_10);
        fxTableNoteBul.getColumnFormatter().setWidth(3, AppMessages.PERCENT_10);
        fxTableNoteBul.getColumnFormatter().setWidth(4, AppMessages.PERCENT_10);
        fxTableNoteBul.getColumnFormatter().setWidth(5, AppMessages.PERCENT_40);
        fxTableNoteBul.getRowFormatter().addStyleName(0, resources.css().headerDescTable());
        panelListNoteBul.setWidth(AppMessages.PERCENT_100);
        panelListNoteBul.add(fxTableNoteBul);

        fxTableParcours.setWidth(AppMessages.PERCENT_100);
        fxTableParcours.setWidget(0, 0, new FormLabel(messages.anneSco()));
        fxTableParcours.setWidget(0, 1, new FormLabel(messages.classeFreq()));
        fxTableParcours.setWidget(0, 2, new FormLabel(messages.moyenneObtenue()));
        fxTableParcours.setWidget(0, 3, new FormLabel(messages.decisionAdmin()));
        fxTableParcours.setWidget(0, 4, new FormLabel(messages.appreciation()));
        fxTableParcours.getColumnFormatter().setWidth(0, AppMessages.PERCENT_15);
        fxTableParcours.getColumnFormatter().setWidth(1, AppMessages.PERCENT_15);
        fxTableParcours.getColumnFormatter().setWidth(2, AppMessages.PERCENT_15);
        fxTableParcours.getColumnFormatter().setWidth(3, AppMessages.PERCENT_15);
        fxTableParcours.getColumnFormatter().setWidth(4, AppMessages.PERCENT_40);
        fxTableParcours.getRowFormatter().addStyleName(0, resources.css().headerDescTable());
        panelListParcours.setWidth(AppMessages.PERCENT_100);
        panelListParcours.add(fxTableParcours);
    }

    void clearFlexTable(FlexTable fxTable) {
       for (int i = fxTable.getRowCount() - 1; i > 0; i--) {
           fxTable.removeRow(i);
       }
    }

    void clearAndInitFlexTable(FlexTable fxTable) {
        clearFlexTable(fxTable);
        for (int i = 0; i < fxTable.getCellCount(0); i++) {
            fxTable.setWidget(1, i, new com.google.gwt.user.client.ui.Label("-"));
        }
     }


    @Override
    public IsWidget getPrincipalContainer() {
        return getWidget();
    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public Map<PANELS_ELEVE_KEYS, Button> getBtnDeplier() {
        final Map<PANELS_ELEVE_KEYS, Button> mapBtn = new HashMap<PANELS_ELEVE_KEYS, Button>();
        mapBtn.put(PANELS_ELEVE_KEYS.IDENTITE, btnDepilerIdentite);
        mapBtn.put(PANELS_ELEVE_KEYS.PARENTS, btnDepilerParents);
        mapBtn.put(PANELS_ELEVE_KEYS.ABS_PERM, btnDepilerAbsPerm);
        mapBtn.put(PANELS_ELEVE_KEYS.FRAIS_SCO, btnDepilerFraisSco);
        mapBtn.put(PANELS_ELEVE_KEYS.NOTE_BUL, btnDepilerNoteBul);
        mapBtn.put(PANELS_ELEVE_KEYS.PARCOURS, btnDepilerParcours);
        return mapBtn;
    }

    @Override
    public void deplier(PANELS_ELEVE_KEYS key) {
        if (key.equals(PANELS_ELEVE_KEYS.IDENTITE)) {
            if (panelIdentite.isVisible()) {
                btnDepilerIdentite.setIcon(IconType.PLUS_SIGN);
                btnEnregistrerIdentite.setEnabled(false);
                btnToutEnregistrerIdentite.setEnabled(false);
                panelIdentite.setVisible(false);
            } else {
                btnDepilerIdentite.setIcon(IconType.MINUS_SIGN);
                btnEnregistrerIdentite.setEnabled(true);
                btnToutEnregistrerIdentite.setEnabled(true);
                panelIdentite.setVisible(true);
            }
        } else if (key.equals(PANELS_ELEVE_KEYS.PARENTS)) {
            if (panelParents.isVisible()) {
                btnDepilerParents.setIcon(IconType.PLUS_SIGN);
                btnEnregistrerParents.setEnabled(false);
                btnToutEnregistrerParents.setEnabled(false);
                panelParents.setVisible(false);
            } else {
                btnDepilerParents.setIcon(IconType.MINUS_SIGN);
                btnEnregistrerParents.setEnabled(true);
                btnToutEnregistrerParents.setEnabled(true);
                panelParents.setVisible(true);
            }
        } else if (key.equals(PANELS_ELEVE_KEYS.ABS_PERM)) {
            if (panelAbsPerm.isVisible()) {
                btnDepilerAbsPerm.setIcon(IconType.PLUS_SIGN);
                btnEnregistrerAbsPerm.setEnabled(false);
                btnToutEnregistrerAbsPerm.setEnabled(false);
                panelAbsPerm.setVisible(false);
            } else {
                btnDepilerAbsPerm.setIcon(IconType.MINUS_SIGN);
                btnEnregistrerAbsPerm.setEnabled(true);
                btnToutEnregistrerAbsPerm.setEnabled(true);
                panelAbsPerm.setVisible(true);
            }
        } else if (key.equals(PANELS_ELEVE_KEYS.FRAIS_SCO)) {
            if (panelFraisSco.isVisible()) {
                btnDepilerFraisSco.setIcon(IconType.PLUS_SIGN);
                btnEnregistrerFraisSco.setEnabled(false);
                btnToutEnregistrerFraisSco.setEnabled(false);
                panelFraisSco.setVisible(false);
            } else {
                btnDepilerFraisSco.setIcon(IconType.MINUS_SIGN);
                btnEnregistrerFraisSco.setEnabled(true);
                btnToutEnregistrerFraisSco.setEnabled(true);
                panelFraisSco.setVisible(true);
            }
        } else if (key.equals(PANELS_ELEVE_KEYS.NOTE_BUL)) {
            if (panelNoteBul.isVisible()) {
                btnDepilerNoteBul.setIcon(IconType.PLUS_SIGN);
                btnEnregistrerNoteBul.setEnabled(false);
                btnToutEnregistrerNoteBul.setEnabled(false);
                panelNoteBul.setVisible(false);
            } else {
                btnDepilerNoteBul.setIcon(IconType.MINUS_SIGN);
                btnEnregistrerNoteBul.setEnabled(true);
                btnToutEnregistrerNoteBul.setEnabled(true);
                panelNoteBul.setVisible(true);
            }
        } else if (key.equals(PANELS_ELEVE_KEYS.PARCOURS)) {
            if (panelParcours.isVisible()) {
                btnDepilerParcours.setIcon(IconType.PLUS_SIGN);
                btnImprimerParcours.setEnabled(false);
                panelParcours.setVisible(false);
            } else {
                btnDepilerParcours.setIcon(IconType.MINUS_SIGN);
                btnImprimerParcours.setEnabled(true);
                panelParcours.setVisible(true);
            }
        }
    }

    @Override
    public void load(EleveDetailsModel eleve, Map<String, IdLabelModel> constantes, PANELS_ELEVE_KEYS key) {
        if (eleve != null) {
            if (key.equals(PANELS_ELEVE_KEYS.IDENTITE)) {
                slbClasse.setValue(eleve.getClasse());
                slbRedoublant.setValue(eleve.getRedoublant());
                slbStatut.setValue(eleve.getStatut());
                tbMatricule.setValue(eleve.getNumber());
                dbDateEntree.setValue(eleve.getDateEntree());
                dbDateSortie.setValue(eleve.getDateSortie());
                slbSexe.setValue(eleve.getSexe());
                tbNom.setValue(eleve.getLastName());
                tbPrenoms.setValue(eleve.getFirstName());
                photoEleve.setUrl(eleve.getUrlPhoto());
                dbDateNais.setValue(eleve.getDateNaissance());
                tbLieuNais.setValue(eleve.getLieuNaissance());
                tbCourriel.setValue(eleve.getCourriel());
                tbAdresse.setValue(eleve.getAdresse());
                tbObservations.setValue(eleve.getObservations());
                if (eleve.getPieces() != null && !eleve.getPieces().isEmpty()) {
                    panelListePJ.clear();
                    for (IdLabelModel piece : eleve.getPieces()) {
                        if (piece != null && piece.getLabel() != null && !"".equals(piece.getLabel())) {
                            panelListePJ.add(new Label(piece.getLabel()));
                        }
                    }
                }
            } else if (key.equals(PANELS_ELEVE_KEYS.PARENTS)) {
                if (eleve.getParents() != null && !eleve.getParents().isEmpty()) {
                    for (ParentModel parent : eleve.getParents()) {
                        if (parent != null && parent.getParentType() != null && parent.getParentType().getId() != null
                                && parent.getParentType().getId().equals(constantes.get(ConstantesKeys.TYPE_PERE_KEY).getId())) {
                            tbNomPere.setValue(parent.getParentNom());
                            tbPrenomsPere.setValue(parent.getParentPrenoms());
                            tbAdressePere.setValue(parent.getParentAdresse());
                            tbTelPere.setValue(parent.getParentTelephone());
                        } else if (parent != null && parent.getParentType() != null && parent.getParentType().getId() != null
                                && parent.getParentType().getId().equals(constantes.get(ConstantesKeys.TYPE_MERE_KEY).getId())) {
                            tbNomMere.setValue(parent.getParentNom());
                            tbPrenomsMere.setValue(parent.getParentPrenoms());
                            tbAdresseMere.setValue(parent.getParentAdresse());
                            tbTelMere.setValue(parent.getParentTelephone());
                        } else if (parent != null && parent.getParentType() != null && parent.getParentType().getId() != null
                                && parent.getParentType().getId().equals(constantes.get(ConstantesKeys.TYPE_TUTEUR_KEY).getId())) {
                            tbNomTuteur.setValue(parent.getParentNom());
                            tbPrenomsTuteur.setValue(parent.getParentPrenoms());
                            tbAdresseTuteur.setValue(parent.getParentAdresse());
                            tbTelTuteur.setValue(parent.getParentTelephone());
                        }
                    }
                }
            } else if (key.equals(PANELS_ELEVE_KEYS.ABS_PERM)) {
                if (eleve.getListAbsPerm() != null && !eleve.getListAbsPerm().isEmpty()) {
                    clearFlexTable(fxTableAbsPerm);
                    int row = 1;
                    for (AbsPermModel absPerm : eleve.getListAbsPerm()) {
                        if (absPerm != null) {
                            final com.google.gwt.user.client.ui.Label type = new com.google.gwt.user.client.ui.Label(
                                (absPerm.getAbsPermType() != null && absPerm.getAbsPermType().getLabel() != null) ? absPerm.getAbsPermType().getLabel() : "");
                            final com.google.gwt.user.client.ui.Label dateDeb = new com.google.gwt.user.client.ui.Label(
                                (absPerm.getDateDebut() != null) ? absPerm.getDateDebut().toString() : "");
                            final com.google.gwt.user.client.ui.Label dateFin = new com.google.gwt.user.client.ui.Label(
                                (absPerm.getDateFin() != null) ? absPerm.getDateFin().toString() : "");
                            final com.google.gwt.user.client.ui.Label justifiee = new com.google.gwt.user.client.ui.Label(
                                (absPerm.getJustifiee() != null && absPerm.getJustifiee().getLabel() != null) ? absPerm.getJustifiee().getLabel() : "");
                            final com.google.gwt.user.client.ui.Label motifs = new com.google.gwt.user.client.ui.Label(
                                (absPerm.getMotifs() != null) ? absPerm.getMotifs() : "");
                            fxTableAbsPerm.setWidget(row, 0, type);
                            fxTableAbsPerm.setWidget(row, 1, dateDeb);
                            fxTableAbsPerm.setWidget(row, 2, dateFin);
                            fxTableAbsPerm.setWidget(row, 3, justifiee);
                            fxTableAbsPerm.setWidget(row, 4, motifs);
                            row++;
                        }
                    }
                }
            } else if (key.equals(PANELS_ELEVE_KEYS.FRAIS_SCO)) {
                if (eleve.getListFraisSco() != null && !eleve.getListFraisSco().isEmpty()) {
                    clearFlexTable(fxTableFraisSco);
                    int row = 1;
                    for (FraisScoModel frais : eleve.getListFraisSco()) {
                        if (frais != null) {
                            final com.google.gwt.user.client.ui.Label tranche = new com.google.gwt.user.client.ui.Label(
                                (frais.getTranche() != null && frais.getTranche().getLabel() != null) ? frais.getTranche().getLabel() : "");
                            final com.google.gwt.user.client.ui.Label datePaiemt = new com.google.gwt.user.client.ui.Label(
                                (frais.getDatePaiement() != null) ? frais.getDatePaiement().toString() : "");
                            final com.google.gwt.user.client.ui.Label montantDu = new com.google.gwt.user.client.ui.Label(
                                ((frais.getMontantDu() != null) ? frais.getMontantDu().toString() : "0") + constantes.get(ConstantesKeys.DEVISE_KEY).getLabel());
                            final com.google.gwt.user.client.ui.Label montantPaye = new com.google.gwt.user.client.ui.Label(
                                ((frais.getMontantPaye() != null) ? frais.getMontantPaye().toString() : "0") + constantes.get(ConstantesKeys.DEVISE_KEY).getLabel());
                            final com.google.gwt.user.client.ui.Label montantRestant = new com.google.gwt.user.client.ui.Label(
                                ((frais.getMontantRestant() != null) ? frais.getMontantRestant().toString() : "0") + constantes.get(ConstantesKeys.DEVISE_KEY).getLabel());
                            fxTableFraisSco.setWidget(row, 0, tranche);
                            fxTableFraisSco.setWidget(row, 1, datePaiemt);
                            fxTableFraisSco.setWidget(row, 2, montantDu);
                            fxTableFraisSco.setWidget(row, 3, montantPaye);
                            fxTableFraisSco.setWidget(row, 4, montantRestant);
                            row++;
                        }
                    }
                }
            } else if (key.equals(PANELS_ELEVE_KEYS.NOTE_BUL)) {
                if (eleve.getListNotes() != null && !eleve.getListNotes().isEmpty()) {
                    clearFlexTable(fxTableNoteBul);
                    int row = 1;
                    for (NoteModel note : eleve.getListNotes()) {
                        if (note != null) {
                            final TextBox matiere = new TextBox();
                            matiere.setValue((note.getMatiere() != null && note.getMatiere().getLabel() != null) ? note.getMatiere().getLabel() : "");
                            final DoubleBox note1 = new DoubleBox();
                            note1.setValue((note.getNote1() != null) ? note.getNote1() : 0.0d);
                            final DoubleBox note2 = new DoubleBox();
                            note2.setValue((note.getNote2() != null) ? note.getNote2() : 0.0d);
                            final DoubleBox note3 = new DoubleBox();
                            note3.setValue((note.getNote3() != null) ? note.getNote3() : 0.0d);
                            final DoubleBox compo = new DoubleBox();
                            compo.setValue((note.getCompo() != null) ? note.getCompo() : 0.0d);
                            final TextBox appreciation = new TextBox();
                            appreciation.setValue((note.getAppreciation() != null) ? note.getAppreciation() : "");
                            fxTableNoteBul.setWidget(row, 0, matiere);
                            fxTableNoteBul.setWidget(row, 1, note1);
                            fxTableNoteBul.setWidget(row, 2, note2);
                            fxTableNoteBul.setWidget(row, 3, note3);
                            fxTableNoteBul.setWidget(row, 4, compo);
                            fxTableNoteBul.setWidget(row, 5, appreciation);
                            row++;
                        }
                    }
                }
            } else if (key.equals(PANELS_ELEVE_KEYS.PARCOURS)) {
                if (eleve.getListParcours() != null && !eleve.getListParcours().isEmpty()) {
                    clearFlexTable(fxTableParcours);
                    int row = 1;
                    for (ParcoursModel parcours : eleve.getListParcours()) {
                        if (parcours != null) {
                            final com.google.gwt.user.client.ui.Label anneeSco = new com.google.gwt.user.client.ui.Label(
                                (parcours.getAnneeSco() != null && parcours.getAnneeSco().getLabel() != null) ? parcours.getAnneeSco().getLabel() : "");
                            final com.google.gwt.user.client.ui.Label classeFreq = new com.google.gwt.user.client.ui.Label(
                                (parcours.getClasseFreq() != null && parcours.getClasseFreq().getLabel() != null) ? parcours.getClasseFreq().getLabel() : "");
                            final com.google.gwt.user.client.ui.Label moyenne = new com.google.gwt.user.client.ui.Label(
                                ((parcours.getMoyenneObtenue() != null) ? parcours.getMoyenneObtenue().toString() : "0"));
                            final com.google.gwt.user.client.ui.Label decision = new com.google.gwt.user.client.ui.Label(
                                (parcours.getDecisionAdmin() != null && parcours.getDecisionAdmin().getLabel() != null) ? parcours.getDecisionAdmin().getLabel() : "");
                            final com.google.gwt.user.client.ui.Label appreciation = new com.google.gwt.user.client.ui.Label(
                                ((parcours.getAppreciations() != null) ? parcours.getAppreciations() : ""));
                            fxTableParcours.setWidget(row, 0, anneeSco);
                            fxTableParcours.setWidget(row, 1, classeFreq);
                            fxTableParcours.setWidget(row, 2, moyenne);
                            fxTableParcours.setWidget(row, 3, decision);
                            fxTableParcours.setWidget(row, 4, appreciation);
                            row++;
                        }
                    }
                }
            }
        }
    }

    @Override
    public void clearPanel(PANELS_ELEVE_KEYS key) {
        if (key.equals(PANELS_ELEVE_KEYS.IDENTITE)) {
            slbClasse.setValue(null);
            slbRedoublant.setValue(null);
            slbStatut.setValue(null);
            tbMatricule.setValue("");
            dbDateEntree.setValue(null);
            dbDateSortie.setValue(null);
            slbSexe.setValue(null);
            tbNom.setValue("");
            tbPrenoms.setValue("");
            photoEleve = new Image(resources.noPhoto());
            dbDateNais.setValue(null);
            tbLieuNais.setValue("");
            tbCourriel.setValue("");
            tbAdresse.setValue("");
            tbObservations.setValue("");
            panelListePJ.clear();
            panelListePJ.add(new Label(messages.lblAucunePiece()));
        } else if (key.equals(PANELS_ELEVE_KEYS.PARENTS)) {
            tbNomPere.setValue("");
            tbPrenomsPere.setValue("");
            tbAdressePere.setValue("");
            tbTelPere.setValue("");
            tbNomMere.setValue("");
            tbPrenomsMere.setValue("");
            tbAdresseMere.setValue("");
            tbTelMere.setValue("");
            tbNomTuteur.setValue("");
            tbPrenomsTuteur.setValue("");
            tbAdresseTuteur.setValue("");
            tbTelTuteur.setValue("");
        } else if (key.equals(PANELS_ELEVE_KEYS.ABS_PERM)) {
            clearAndInitFlexTable(fxTableAbsPerm);
        } else if (key.equals(PANELS_ELEVE_KEYS.FRAIS_SCO)) {
            clearAndInitFlexTable(fxTableFraisSco);
        } else if (key.equals(PANELS_ELEVE_KEYS.NOTE_BUL)) {
            clearAndInitFlexTable(fxTableNoteBul);
        } else if (key.equals(PANELS_ELEVE_KEYS.PARCOURS)) {
            clearAndInitFlexTable(fxTableParcours);
        }
    }

    @Override
    public Map<PANELS_ELEVE_KEYS, SuggestListBoxSingle<IdLabelModel>> getSlbPeriode() {
        final Map<PANELS_ELEVE_KEYS, SuggestListBoxSingle<IdLabelModel>> mapSlb = new HashMap<PANELS_ELEVE_KEYS, SuggestListBoxSingle<IdLabelModel>>();
        mapSlb.put(PANELS_ELEVE_KEYS.ABS_PERM, slbPeriodeAbsPerm);
        mapSlb.put(PANELS_ELEVE_KEYS.FRAIS_SCO, slbPeriodeFraisSco);
        mapSlb.put(PANELS_ELEVE_KEYS.NOTE_BUL, slbPeriodeNoteBul);
        return mapSlb;
    }

    @Override
    public SuggestListBoxSingle<IdLabelModel> getSlbClasse() {
        return slbClasse;
    }

    @Override
    public SuggestListBoxSingle<IdLabelModel> getSlbRedoublant() {
        return slbRedoublant;
    }

    @Override
    public SuggestListBoxSingle<IdLabelModel> getSlbSexe() {
        return slbSexe;
    }

    @Override
    public SuggestListBoxSingle<IdLabelModel> getSlbStatut() {
        return slbStatut;
    }

}
